package com.wangyang.stock.business.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.wangyang.stock.business.IOperatorService;
import com.wangyang.stock.dao.IDao;
import com.wangyang.stock.po.Employee;
import com.wangyang.stock.po.Operator;
import com.wangyang.stock.po.Role;
import com.wangyang.stock.vo.OperatorVo;

public class OperatorServiceImp implements IOperatorService {

	private IDao dao;

	public IDao getDao() {
		return dao;
	}

	public void setDao(IDao dao) {
		this.dao = dao;
	}

	public Operator getOperator(Operator operator) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(
				"from Operator opr where opr.operatorName = '");
		hql.append(operator.getOperatorName());
		hql.append("' and opr.operatorPass = '");
		hql.append(operator.getOperatorPass());
		hql.append("'");

		return (Operator) dao.select(hql.toString()).get(0);
	}

	public List selectOperator(OperatorVo operatorVo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from Operator opr where 1 = 1");
		List list = dao.select(hql.toString());
		List volist = new ArrayList();

		for (int i = 0; i < list.size(); i++) {
			OperatorVo operVo = new OperatorVo();
			Operator oper = (Operator) list.get(i);
			operVo.setOperatorId(oper.getOperatorId());
			operVo.setOperatorCid(oper.getOperatorCid());
			operVo.setOperatorName(oper.getOperatorName());
			operVo.setOperatorPass(oper.getOperatorPass());
			if (null != oper.getEmployee()) {
				operVo.setEmployeeId(oper.getEmployee().getEmployeeId());
				operVo.setEmployeeName(oper.getEmployee().getEmployeeName());
			}
			if (null != oper.getRole()) {
				operVo.setRoleId(oper.getRole().getRoleId());
				operVo.setRoleName(oper.getRole().getRoleName());
			}
			volist.add(operVo);
		}
		return volist;
	}

	public boolean isLogin(List<Operator> loginUsers, Operator opr) {
		// TODO Auto-generated method stub
		boolean is = true;
		for (int i = 0; i < loginUsers.size(); i++) {
			Operator loginOpr = loginUsers.get(i);
			if (loginOpr.equals(opr)) {
				// 返回该用户已登陆
				is = false;
				break;
			}
		}
		return is;
	}

	public boolean updateOperator(OperatorVo operatorVo) {
		// TODO Auto-generated method stub
		Operator oper = new Operator();
		BeanUtils.copyProperties(operatorVo, oper);
		oper.setEmployee(new Employee(operatorVo.getEmployeeId()));
		oper.setRole(new Role(operatorVo.getRoleId()));
		boolean is = false;
		if (dao.update(oper)) {
			is = true;
		}
		return is;
	}

}
